package com.android.phoenix.fragments.userinfoimprove;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;
import com.android.phoenix.fragments.FragmentSwitchListener;

public class UserInfoHeightFragment extends Fragment implements
		OnClickListener, OnValueChangeListener, OnScrollListener {
	private static final String TAG = "UserInfoHeightFragment";

	/** 事件监听 */
	private FragmentSwitchListener listener;

	private Button btnUserInfoHeightLast = null;
	private Button btnUserInfoHeightNext = null;
	private NumberPicker numpUserHeight = null;

	private int userHeight = 170;

	@Override
	public void onAttach(Activity activity) {
		try {
			listener = (FragmentSwitchListener) activity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fra_userinfoheight, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		initViews();
	}

	/**
	 * @Title: initViews
	 * @Description: 控件初始化
	 * @author: WanChao
	 * @date: 2015-1-7 下午10:26:33
	 */
	private void initViews() {
		numpUserHeight = (NumberPicker) getActivity().findViewById(
				R.id.numpUserHeight);
		numpUserHeight.setMaxValue(200);
		numpUserHeight.setMinValue(140);
		numpUserHeight.setValue(170);
		numpUserHeight.getChildAt(0).setFocusable(false);
		numpUserHeight.setOnValueChangedListener(this);
		numpUserHeight.setOnScrollListener(this);

		btnUserInfoHeightLast = (Button) getActivity().findViewById(
				R.id.btnUserInfoHeightLast);
		btnUserInfoHeightNext = (Button) getActivity().findViewById(
				R.id.btnUserInfoHeightNext);
		btnUserInfoHeightLast.setOnClickListener(this);
		btnUserInfoHeightNext.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnUserInfoHeightLast:
			listener.fragmentSwitch(UserInfoHeightFragment.class.getName(),
					UserInfoAgeFragment.class.getName(), null);
			break;
		case R.id.btnUserInfoHeightNext:
			saveUserHeight();
			listener.fragmentSwitch(UserInfoHeightFragment.class.getName(),
					UserInfoWeightFragment.class.getName(), null);
			break;
		}
	}

	private void saveUserHeight() {
		MainApplication.mSharedPreferences.edit()
				.putInt(Constant.USERHEIGHT, userHeight).commit();
	}

	@Override
	public void onScrollStateChange(NumberPicker arg0, int scrollState) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onValueChange(NumberPicker arg0, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		userHeight = newValue;
	}
}
